package vn.student.mylib;

import java.util.ArrayList;
import vn.student.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListContactAdapter extends ArrayAdapter<Contact>{
	Activity act;
	public ArrayList<Contact> list;
	boolean isHaveAddNewBtn = false;
	public ListContactAdapter(Activity act,View view, ArrayList<Contact> list) {
		super(act, R.layout.instructor_row,list);
		this.act=act;
		this.list = list;
		if(list!=null&&list.size()>0){
			ListView tmp = (ListView)view.findViewById(R.id.contactListView);
			tmp.getLayoutParams().height=list.size()*38+10;
		}
	}
	public ListContactAdapter(Activity act, ArrayList<Contact> listPartner) {
		super(act, R.layout.instructor_row,listPartner);
		this.act=act;
		this.list = listPartner;
		this.isHaveAddNewBtn = true;
		if(list!=null&&list.size()>0){
			RelativeLayout rl = (RelativeLayout) act.findViewById(R.id.addPartnerBtn);
			rl.setBackgroundResource(R.drawable.class_row_top);
			ListView tmp = (ListView)act.findViewById(R.id.contactListView);
			tmp.getLayoutParams().height=list.size()*38+10;
		}
	}
	public View getView(int position, View convertView,ViewGroup parent) {
		 View row=convertView;
		 RowWrapper wrapper=null;
	     if (row==null) {                         
	    	 LayoutInflater inflater=act.getLayoutInflater();
	        
	    	 row=inflater.inflate(R.layout.instructor_row, null);
	    	 wrapper=new RowWrapper(row);
	    	 row.setTag(wrapper);
	     }
	     else {
	    	 wrapper=(RowWrapper)row.getTag();
	     }
	     Contact instructor = list.get(position);
	     wrapper.getInstructorNameLabel().setText(instructor.name);
	     if(list.size()==1){
			 if(isHaveAddNewBtn)
				 row.setBackgroundResource(R.drawable.class_row_bottom);
			 else
				 row.setBackgroundResource(R.drawable.class_row_all);
		 }
		 else{
			 if(position==(list.size()-1)){
				 row.setBackgroundResource(R.drawable.class_row_bottom);
			 }
			 else 
				 if(position==0&&(!isHaveAddNewBtn))
					 row.setBackgroundResource(R.drawable.class_row_top);
				 else
					 row.setBackgroundResource(R.drawable.class_row_none);
		 }
	     return(row);
	}
	private class RowWrapper {
		View base;
		TextView instructorName = null;
	  
		RowWrapper(View base) {
			this.base=base;
		}
		TextView getInstructorNameLabel() {
			if (instructorName==null) {
				instructorName=(TextView)base.findViewById(R.id.instructorName);
			}
		  return(instructorName);
		}
	}
}

